package net.jxta.myjxta.ui;

import net.jxta.myjxta.plugin.PluginContainer;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class UIHelper {


    public static class PopupConverterEntry {
        public PopupConverterEntry(Action p_action, PluginContainer.MenuPath[] p_path) {
            m_action = p_action;
            path = p_path;
        }

        public PluginContainer.MenuPath[] path = null;
        public Action m_action = null;
    }

    public static PluginContainer.MenuPath[] expandPath(PluginContainer.MenuPath[] old, PluginContainer.MenuPath newOne) {
        int oldLenght = old != null ? old.length : 0;
        PluginContainer.MenuPath result[] = new PluginContainer.MenuPath[oldLenght + 1];
        if (old != null) {
            System.arraycopy(old, 0, result, 0, old.length);
        }
        result[result.length - 1] = newOne;
        return result;
    }

    public static void getPopupsFromPopupMenu(JPopupMenu p_oldMenu, PluginContainer.MenuPath[] p_currentPath, ArrayList<PopupConverterEntry> result) {


        Component[] toplevelcomps = p_oldMenu.getComponents();
        for (int i = 0; i < toplevelcomps.length; i++) {
            Component menuComp = toplevelcomps[i];
            if (menuComp instanceof JMenuItem) {
                getPopupFromJMenuItem(menuComp, p_currentPath, result);
            }
        }
    }

    public static void getPopupFromJMenuItem(Component p_menuComp, PluginContainer.MenuPath[] p_currentPath, ArrayList<PopupConverterEntry> result) {
        JMenuItem jMenuItem = (JMenuItem) p_menuComp;
        if (jMenuItem instanceof JMenu) {
            //another menu
            JMenu jMenu = (JMenu) jMenuItem;
            if (jMenu.isEnabled()) {
                Component[] comps = jMenu.getMenuComponents();
                for (int i = 0; i < comps.length; i++) {
                    Component comp = comps[i];
                    getPopupFromJMenuItem(comp, expandPath(p_currentPath, new PluginContainer.MenuPath(jMenu.getText(), jMenu.getMnemonic())), result);
                }
            }
        } else {
            JMenuItem menuItem = (JMenuItem) jMenuItem;
            Action action = menuItem.getAction();
            action.setEnabled(menuItem.isEnabled());
            result.add(new PopupConverterEntry(action, p_currentPath));
        }
    }

    public static JMenu getOrCreateMenuForPath(JComponent root, PluginContainer.MenuPath[] p_path) {
        if (!(root instanceof JPopupMenu || root instanceof JMenu)) {
            return null;
        }

        if (p_path == null || p_path.length == 0) {
            return null;
        } else {
            Component[] toplevels;
            if (root instanceof JPopupMenu) {
                toplevels = root.getComponents();
            } else if (root instanceof JMenu) {
                toplevels = ((JMenu) root).getMenuComponents();
            } else
                return null;

            for (int i = 0; i < toplevels.length; i++) {
                Component toplevel = toplevels[i];
                if (toplevel instanceof JMenu) {
                    JMenu jMenu = (JMenu) toplevel;
                    if (jMenu.getText().equals(p_path[0].label)) {
                        if (p_path.length == 1) {
                            return jMenu;
                        } else {
                            PluginContainer.MenuPath[] newPath = new PluginContainer.MenuPath[p_path.length - 1];
                            System.arraycopy(p_path, 1, newPath, 0, newPath.length);
                            return getOrCreateMenuForPath(jMenu, newPath);
                        }
                    } else {

                    }
                }
            }
            //no menu found for p_path[0]


            JComponent newRoot = null;
            if (root instanceof JPopupMenu) {
                newRoot = (JComponent) root.add(new JMenu(p_path[0].label));
            } else {
                //JMenu
                newRoot = ((JMenu) root).add(new JMenu(p_path[0].label));
            }
            PluginContainer.MenuPath[] newPath = new PluginContainer.MenuPath[p_path.length - 1];
            System.arraycopy(p_path, 1, newPath, 0, newPath.length);
            if (newPath.length == 0) {
                return (JMenu) newRoot;
            } else {
                return getOrCreateMenuForPath(newRoot, newPath);
            }
        }

    }
}
